{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Approximate Features\n",
    "\n",
    "\n",
    "Some feature are more computationally intensive to calculate than others.  In a large dataset, direct features that are aggregations on the prediction entity may not change much from cutoff time to cutoff time. Calculating the aggregation features at specific times every hour and using it for all cutoff times within the hour would save time and perhaps not lose much information.  The approximate parameter in calculate_feature_matrix and dfs let's you specify a window size to use when approximating these direct aggregation features.  This example will showcase how to use this feature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import featuretools as ft\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The flight dataset is about 1.6 GB in size, so you may want to use the nrows paramater to if the size is too great."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# es = ft.demo.load_flight()\n",
    "es = ft.demo.load_flight(nrows=200000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With the entityset loaded, let's use dfs to get create some features to calculate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Entityset: flight_dataset\n",
       "  Entities:\n",
       "    flights (shape = [141436, 9])\n",
       "    trips (shape = [200000, 24])\n",
       "  Relationships:\n",
       "    trips.flight_id -> flights.flight_id"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "es"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trip_id</th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>FL_DATE</th>\n",
       "      <th>CRS_DEP_TIME</th>\n",
       "      <th>DEP_TIME</th>\n",
       "      <th>TAXI_OUT</th>\n",
       "      <th>WHEELS_OFF</th>\n",
       "      <th>WHEELS_ON</th>\n",
       "      <th>TAXI_IN</th>\n",
       "      <th>CRS_ARR_TIME</th>\n",
       "      <th>...</th>\n",
       "      <th>CRS_ELAPSED_TIME</th>\n",
       "      <th>ACTUAL_ELAPSED_TIME</th>\n",
       "      <th>DISTANCE</th>\n",
       "      <th>CARRIER_DELAY</th>\n",
       "      <th>WEATHER_DELAY</th>\n",
       "      <th>NAS_DELAY</th>\n",
       "      <th>SECURITY_DELAY</th>\n",
       "      <th>LATE_AIRCRAFT_DELAY</th>\n",
       "      <th>Unnamed: 27</th>\n",
       "      <th>flight_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trip_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>615</td>\n",
       "      <td>614.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>636.0</td>\n",
       "      <td>1258.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1325</td>\n",
       "      <td>...</td>\n",
       "      <td>250.0</td>\n",
       "      <td>229.0</td>\n",
       "      <td>1892.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DL_N818DA_1592</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2316</td>\n",
       "      <td>2307.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>2322.0</td>\n",
       "      <td>635.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>653</td>\n",
       "      <td>...</td>\n",
       "      <td>277.0</td>\n",
       "      <td>276.0</td>\n",
       "      <td>2182.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DL_N831DN_1593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2215</td>\n",
       "      <td>2212.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>2235.0</td>\n",
       "      <td>2323.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2328</td>\n",
       "      <td>...</td>\n",
       "      <td>133.0</td>\n",
       "      <td>134.0</td>\n",
       "      <td>674.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DL_N905DL_1594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>1644</td>\n",
       "      <td>1639.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>1655.0</td>\n",
       "      <td>1922.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>1955</td>\n",
       "      <td>...</td>\n",
       "      <td>251.0</td>\n",
       "      <td>231.0</td>\n",
       "      <td>1547.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DL_N698DL_1595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>1930</td>\n",
       "      <td>1952.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>2043.0</td>\n",
       "      <td>2342.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2312</td>\n",
       "      <td>...</td>\n",
       "      <td>162.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>1020.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DL_N982AT_1596</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         trip_id  Unnamed: 0    FL_DATE  CRS_DEP_TIME  DEP_TIME  TAXI_OUT  \\\n",
       "trip_id                                                                     \n",
       "0              0           0 2016-02-01           615     614.0      22.0   \n",
       "1              1           1 2016-02-01          2316    2307.0      15.0   \n",
       "2              2           2 2016-02-01          2215    2212.0      23.0   \n",
       "3              3           3 2016-02-01          1644    1639.0      16.0   \n",
       "4              4           4 2016-02-01          1930    1952.0      51.0   \n",
       "\n",
       "         WHEELS_OFF  WHEELS_ON  TAXI_IN  CRS_ARR_TIME       ...        \\\n",
       "trip_id                                                     ...         \n",
       "0             636.0     1258.0      5.0          1325       ...         \n",
       "1            2322.0      635.0      8.0           653       ...         \n",
       "2            2235.0     2323.0      3.0          2328       ...         \n",
       "3            1655.0     1922.0      8.0          1955       ...         \n",
       "4            2043.0     2342.0      6.0          2312       ...         \n",
       "\n",
       "         CRS_ELAPSED_TIME  ACTUAL_ELAPSED_TIME DISTANCE  CARRIER_DELAY  \\\n",
       "trip_id                                                                  \n",
       "0                   250.0                229.0   1892.0            NaN   \n",
       "1                   277.0                276.0   2182.0            NaN   \n",
       "2                   133.0                134.0    674.0            NaN   \n",
       "3                   251.0                231.0   1547.0            NaN   \n",
       "4                   162.0                176.0   1020.0           22.0   \n",
       "\n",
       "         WEATHER_DELAY  NAS_DELAY  SECURITY_DELAY  LATE_AIRCRAFT_DELAY  \\\n",
       "trip_id                                                                  \n",
       "0                  NaN        NaN             NaN                  NaN   \n",
       "1                  NaN        NaN             NaN                  NaN   \n",
       "2                  NaN        NaN             NaN                  NaN   \n",
       "3                  NaN        NaN             NaN                  NaN   \n",
       "4                  0.0       14.0             0.0                  0.0   \n",
       "\n",
       "         Unnamed: 27       flight_id  \n",
       "trip_id                               \n",
       "0                NaN  DL_N818DA_1592  \n",
       "1                NaN  DL_N831DN_1593  \n",
       "2                NaN  DL_N905DL_1594  \n",
       "3                NaN  DL_N698DL_1595  \n",
       "4                NaN  DL_N982AT_1596  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trips = es['trips'].df\n",
    "trips.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>trip_id</th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>FL_DATE</th>\n",
       "      <th>CRS_DEP_TIME</th>\n",
       "      <th>DEP_TIME</th>\n",
       "      <th>TAXI_OUT</th>\n",
       "      <th>WHEELS_OFF</th>\n",
       "      <th>WHEELS_ON</th>\n",
       "      <th>TAXI_IN</th>\n",
       "      <th>CRS_ARR_TIME</th>\n",
       "      <th>...</th>\n",
       "      <th>CRS_ELAPSED_TIME</th>\n",
       "      <th>ACTUAL_ELAPSED_TIME</th>\n",
       "      <th>DISTANCE</th>\n",
       "      <th>CARRIER_DELAY</th>\n",
       "      <th>WEATHER_DELAY</th>\n",
       "      <th>NAS_DELAY</th>\n",
       "      <th>SECURITY_DELAY</th>\n",
       "      <th>LATE_AIRCRAFT_DELAY</th>\n",
       "      <th>Unnamed: 27</th>\n",
       "      <th>flight_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trip_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>199995</th>\n",
       "      <td>199995</td>\n",
       "      <td>199995</td>\n",
       "      <td>2016-02-14</td>\n",
       "      <td>1050</td>\n",
       "      <td>1042.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>1059.0</td>\n",
       "      <td>1325.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1350</td>\n",
       "      <td>...</td>\n",
       "      <td>120.0</td>\n",
       "      <td>113.0</td>\n",
       "      <td>667.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N13997_4220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199996</th>\n",
       "      <td>199996</td>\n",
       "      <td>199996</td>\n",
       "      <td>2016-02-14</td>\n",
       "      <td>657</td>\n",
       "      <td>647.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>707.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>810</td>\n",
       "      <td>...</td>\n",
       "      <td>73.0</td>\n",
       "      <td>75.0</td>\n",
       "      <td>253.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14105_4221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199997</th>\n",
       "      <td>199997</td>\n",
       "      <td>199997</td>\n",
       "      <td>2016-02-14</td>\n",
       "      <td>900</td>\n",
       "      <td>855.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>908.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1030</td>\n",
       "      <td>...</td>\n",
       "      <td>90.0</td>\n",
       "      <td>82.0</td>\n",
       "      <td>489.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N19554_4221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199998</th>\n",
       "      <td>199998</td>\n",
       "      <td>199998</td>\n",
       "      <td>2016-02-14</td>\n",
       "      <td>1015</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>1022.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1333</td>\n",
       "      <td>...</td>\n",
       "      <td>138.0</td>\n",
       "      <td>127.0</td>\n",
       "      <td>844.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N16911_4222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199999</th>\n",
       "      <td>199999</td>\n",
       "      <td>199999</td>\n",
       "      <td>2016-02-14</td>\n",
       "      <td>620</td>\n",
       "      <td>611.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>621.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>758</td>\n",
       "      <td>...</td>\n",
       "      <td>98.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>427.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14171_4223</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         trip_id  Unnamed: 0    FL_DATE  CRS_DEP_TIME  DEP_TIME  TAXI_OUT  \\\n",
       "trip_id                                                                     \n",
       "199995    199995      199995 2016-02-14          1050    1042.0      17.0   \n",
       "199996    199996      199996 2016-02-14           657     647.0      20.0   \n",
       "199997    199997      199997 2016-02-14           900     855.0      13.0   \n",
       "199998    199998      199998 2016-02-14          1015    1011.0      11.0   \n",
       "199999    199999      199999 2016-02-14           620     611.0      10.0   \n",
       "\n",
       "         WHEELS_OFF  WHEELS_ON  TAXI_IN  CRS_ARR_TIME       ...        \\\n",
       "trip_id                                                     ...         \n",
       "199995       1059.0     1325.0     10.0          1350       ...         \n",
       "199996        707.0      754.0      8.0           810       ...         \n",
       "199997        908.0     1011.0      6.0          1030       ...         \n",
       "199998       1022.0     1312.0      6.0          1333       ...         \n",
       "199999        621.0      734.0      9.0           758       ...         \n",
       "\n",
       "         CRS_ELAPSED_TIME  ACTUAL_ELAPSED_TIME DISTANCE  CARRIER_DELAY  \\\n",
       "trip_id                                                                  \n",
       "199995              120.0                113.0    667.0            NaN   \n",
       "199996               73.0                 75.0    253.0            NaN   \n",
       "199997               90.0                 82.0    489.0            NaN   \n",
       "199998              138.0                127.0    844.0            NaN   \n",
       "199999               98.0                 92.0    427.0            NaN   \n",
       "\n",
       "         WEATHER_DELAY  NAS_DELAY  SECURITY_DELAY  LATE_AIRCRAFT_DELAY  \\\n",
       "trip_id                                                                  \n",
       "199995             NaN        NaN             NaN                  NaN   \n",
       "199996             NaN        NaN             NaN                  NaN   \n",
       "199997             NaN        NaN             NaN                  NaN   \n",
       "199998             NaN        NaN             NaN                  NaN   \n",
       "199999             NaN        NaN             NaN                  NaN   \n",
       "\n",
       "         Unnamed: 27       flight_id  \n",
       "trip_id                               \n",
       "199995           NaN  EV_N13997_4220  \n",
       "199996           NaN  EV_N14105_4221  \n",
       "199997           NaN  EV_N19554_4221  \n",
       "199998           NaN  EV_N16911_4222  \n",
       "199999           NaN  EV_N14171_4223  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trips.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "features = ft.dfs(entityset=es, target_entity='trips', features_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cutoff_time = trips.filter(['trip_id', 'FL_DATE'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we time calculate_feature_matrix using the cutoff times and features."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "calculate_feature_matrix: 100%|██████████| 13/13 [06:53<00:00, 42.40s/it]\n",
      "CPU times: user 6min 54s, sys: 0 ns, total: 6min 54s\n",
      "Wall time: 6min 53s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "feature_matrix = ft.calculate_feature_matrix(features=features, entityset=es,\n",
    "                                             cutoff_time=cutoff_time,\n",
    "                                             verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The number of tasks in the calculate_feature_matrix progress bar refers to the number of unique dates features are being calculated at. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>LATE_AIRCRAFT_DELAY</th>\n",
       "      <th>flight_id</th>\n",
       "      <th>MONTH(FL_DATE)</th>\n",
       "      <th>NAS_DELAY</th>\n",
       "      <th>TAXI_OUT</th>\n",
       "      <th>YEAR(FL_DATE)</th>\n",
       "      <th>ACTUAL_ELAPSED_TIME</th>\n",
       "      <th>WHEELS_ON</th>\n",
       "      <th>Unnamed: 27</th>\n",
       "      <th>...</th>\n",
       "      <th>flights.MEAN(trips.DISTANCE)</th>\n",
       "      <th>flights.MIN(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MIN(trips.NAS_DELAY)</th>\n",
       "      <th>flights.SKEW(trips.Unnamed: 0)</th>\n",
       "      <th>flights.COUNT(trips)</th>\n",
       "      <th>flights.SUM(trips.CRS_ARR_TIME)</th>\n",
       "      <th>flights.SKEW(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MAX(trips.SECURITY_DELAY)</th>\n",
       "      <th>flights.MEAN(trips.Unnamed: 27)</th>\n",
       "      <th>flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>instance_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>199995</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N13997_4220</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>113.0</td>\n",
       "      <td>1325.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>667.0</td>\n",
       "      <td>1157.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2507</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199996</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14105_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>75.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>253.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>810</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199997</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N19554_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>82.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>489.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1030</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199998</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N16911_4222</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>127.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>844.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199999</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14171_4223</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>92.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>758</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 163 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             CANCELLED  LATE_AIRCRAFT_DELAY       flight_id  MONTH(FL_DATE)  \\\n",
       "instance_id                                                                   \n",
       "199995             0.0                  NaN  EV_N13997_4220               2   \n",
       "199996             0.0                  NaN  EV_N14105_4221               2   \n",
       "199997             0.0                  NaN  EV_N19554_4221               2   \n",
       "199998             0.0                  NaN  EV_N16911_4222               2   \n",
       "199999             0.0                  NaN  EV_N14171_4223               2   \n",
       "\n",
       "             NAS_DELAY  TAXI_OUT  YEAR(FL_DATE)  ACTUAL_ELAPSED_TIME  \\\n",
       "instance_id                                                            \n",
       "199995             NaN      17.0           2016                113.0   \n",
       "199996             NaN      20.0           2016                 75.0   \n",
       "199997             NaN      13.0           2016                 82.0   \n",
       "199998             NaN      11.0           2016                127.0   \n",
       "199999             NaN      10.0           2016                 92.0   \n",
       "\n",
       "             WHEELS_ON  Unnamed: 27                    ...                     \\\n",
       "instance_id                                            ...                      \n",
       "199995          1325.0          NaN                    ...                      \n",
       "199996           754.0          NaN                    ...                      \n",
       "199997          1011.0          NaN                    ...                      \n",
       "199998          1312.0          NaN                    ...                      \n",
       "199999           734.0          NaN                    ...                      \n",
       "\n",
       "             flights.MEAN(trips.DISTANCE)  flights.MIN(trips.WHEELS_ON)  \\\n",
       "instance_id                                                               \n",
       "199995                              667.0                        1157.0   \n",
       "199996                              253.0                         754.0   \n",
       "199997                              489.0                        1011.0   \n",
       "199998                              844.0                        1312.0   \n",
       "199999                              427.0                         734.0   \n",
       "\n",
       "             flights.MIN(trips.NAS_DELAY)  flights.SKEW(trips.Unnamed: 0)  \\\n",
       "instance_id                                                                 \n",
       "199995                                NaN                             0.0   \n",
       "199996                                NaN                             0.0   \n",
       "199997                                NaN                             0.0   \n",
       "199998                                NaN                             0.0   \n",
       "199999                                NaN                             0.0   \n",
       "\n",
       "             flights.COUNT(trips)  flights.SUM(trips.CRS_ARR_TIME)  \\\n",
       "instance_id                                                          \n",
       "199995                          2                             2507   \n",
       "199996                          1                              810   \n",
       "199997                          1                             1030   \n",
       "199998                          1                             1333   \n",
       "199999                          1                              758   \n",
       "\n",
       "            flights.SKEW(trips.WHEELS_ON)  flights.MAX(trips.SECURITY_DELAY)  \\\n",
       "instance_id                                                                    \n",
       "199995                                0.0                                NaN   \n",
       "199996                                0.0                                NaN   \n",
       "199997                                0.0                                NaN   \n",
       "199998                                0.0                                NaN   \n",
       "199999                                0.0                                NaN   \n",
       "\n",
       "             flights.MEAN(trips.Unnamed: 27)  \\\n",
       "instance_id                                    \n",
       "199995                                   NaN   \n",
       "199996                                   NaN   \n",
       "199997                                   NaN   \n",
       "199998                                   NaN   \n",
       "199999                                   NaN   \n",
       "\n",
       "             flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))  \n",
       "instance_id                                            \n",
       "199995                                              2  \n",
       "199996                                              1  \n",
       "199997                                              1  \n",
       "199998                                              1  \n",
       "199999                                              1  \n",
       "\n",
       "[5 rows x 163 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_matrix.tail(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And time it again using the approximate parameter "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "approximate_features: 100%|██████████| 5/5 [06:33<00:00, 84.97s/it] \n",
      "calculate_feature_matrix: 100%|██████████| 13/13 [00:05<00:00,  1.83it/s]\n",
      "CPU times: user 6min 42s, sys: 0 ns, total: 6min 42s\n",
      "Wall time: 6min 41s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "feature_matrix_approximated = ft.calculate_feature_matrix(features=features, entityset=es, \n",
    "                                                          cutoff_time=cutoff_time,\n",
    "                                                          approximate=ft.Timedelta(3, 'd'),\n",
    "                                                          verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The number of tasks in the approximate features bar refers to the number of unique dates approximate features are being calculated at.  This is smaller than the number in the non-approximated calculate_feature_matrix, due to how multiple dates are grouped together for approximation.  Notice that the final features are calculated on the same number of dates as before, but the time needed for those calculations is much faster due to the approximation step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>LATE_AIRCRAFT_DELAY</th>\n",
       "      <th>flight_id</th>\n",
       "      <th>MONTH(FL_DATE)</th>\n",
       "      <th>NAS_DELAY</th>\n",
       "      <th>TAXI_OUT</th>\n",
       "      <th>YEAR(FL_DATE)</th>\n",
       "      <th>ACTUAL_ELAPSED_TIME</th>\n",
       "      <th>WHEELS_ON</th>\n",
       "      <th>Unnamed: 27</th>\n",
       "      <th>...</th>\n",
       "      <th>flights.MEAN(trips.DISTANCE)</th>\n",
       "      <th>flights.MIN(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MIN(trips.NAS_DELAY)</th>\n",
       "      <th>flights.SKEW(trips.Unnamed: 0)</th>\n",
       "      <th>flights.COUNT(trips)</th>\n",
       "      <th>flights.SUM(trips.CRS_ARR_TIME)</th>\n",
       "      <th>flights.SKEW(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MAX(trips.SECURITY_DELAY)</th>\n",
       "      <th>flights.MEAN(trips.Unnamed: 27)</th>\n",
       "      <th>flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>instance_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>199995</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N13997_4220</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>113.0</td>\n",
       "      <td>1325.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>667.0</td>\n",
       "      <td>1157.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2507</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199996</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14105_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>75.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>253.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>810</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199997</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N19554_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>82.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>489.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1030</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199998</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N16911_4222</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>127.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>844.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199999</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14171_4223</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>92.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>758</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 163 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             CANCELLED  LATE_AIRCRAFT_DELAY       flight_id  MONTH(FL_DATE)  \\\n",
       "instance_id                                                                   \n",
       "199995             0.0                  NaN  EV_N13997_4220               2   \n",
       "199996             0.0                  NaN  EV_N14105_4221               2   \n",
       "199997             0.0                  NaN  EV_N19554_4221               2   \n",
       "199998             0.0                  NaN  EV_N16911_4222               2   \n",
       "199999             0.0                  NaN  EV_N14171_4223               2   \n",
       "\n",
       "             NAS_DELAY  TAXI_OUT  YEAR(FL_DATE)  ACTUAL_ELAPSED_TIME  \\\n",
       "instance_id                                                            \n",
       "199995             NaN      17.0           2016                113.0   \n",
       "199996             NaN      20.0           2016                 75.0   \n",
       "199997             NaN      13.0           2016                 82.0   \n",
       "199998             NaN      11.0           2016                127.0   \n",
       "199999             NaN      10.0           2016                 92.0   \n",
       "\n",
       "             WHEELS_ON  Unnamed: 27                    ...                     \\\n",
       "instance_id                                            ...                      \n",
       "199995          1325.0          NaN                    ...                      \n",
       "199996           754.0          NaN                    ...                      \n",
       "199997          1011.0          NaN                    ...                      \n",
       "199998          1312.0          NaN                    ...                      \n",
       "199999           734.0          NaN                    ...                      \n",
       "\n",
       "             flights.MEAN(trips.DISTANCE)  flights.MIN(trips.WHEELS_ON)  \\\n",
       "instance_id                                                               \n",
       "199995                              667.0                        1157.0   \n",
       "199996                              253.0                         754.0   \n",
       "199997                              489.0                        1011.0   \n",
       "199998                              844.0                        1312.0   \n",
       "199999                              427.0                         734.0   \n",
       "\n",
       "             flights.MIN(trips.NAS_DELAY)  flights.SKEW(trips.Unnamed: 0)  \\\n",
       "instance_id                                                                 \n",
       "199995                                NaN                             0.0   \n",
       "199996                                NaN                             0.0   \n",
       "199997                                NaN                             0.0   \n",
       "199998                                NaN                             0.0   \n",
       "199999                                NaN                             0.0   \n",
       "\n",
       "             flights.COUNT(trips)  flights.SUM(trips.CRS_ARR_TIME)  \\\n",
       "instance_id                                                          \n",
       "199995                          2                             2507   \n",
       "199996                          1                              810   \n",
       "199997                          1                             1030   \n",
       "199998                          1                             1333   \n",
       "199999                          1                              758   \n",
       "\n",
       "            flights.SKEW(trips.WHEELS_ON)  flights.MAX(trips.SECURITY_DELAY)  \\\n",
       "instance_id                                                                    \n",
       "199995                                0.0                                NaN   \n",
       "199996                                0.0                                NaN   \n",
       "199997                                0.0                                NaN   \n",
       "199998                                0.0                                NaN   \n",
       "199999                                0.0                                NaN   \n",
       "\n",
       "             flights.MEAN(trips.Unnamed: 27)  \\\n",
       "instance_id                                    \n",
       "199995                                   NaN   \n",
       "199996                                   NaN   \n",
       "199997                                   NaN   \n",
       "199998                                   NaN   \n",
       "199999                                   NaN   \n",
       "\n",
       "             flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))  \n",
       "instance_id                                            \n",
       "199995                                              2  \n",
       "199996                                              1  \n",
       "199997                                              1  \n",
       "199998                                              1  \n",
       "199999                                              1  \n",
       "\n",
       "[5 rows x 163 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_matrix_approximated.tail(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "approximate_features: 100%|██████████| 3/3 [06:08<00:00, 118.69s/it]\n",
      "calculate_feature_matrix: 100%|██████████| 13/13 [00:05<00:00,  1.81it/s]\n",
      "CPU times: user 6min 17s, sys: 608 ms, total: 6min 17s\n",
      "Wall time: 6min 16s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "feature_matrix_approximated_2 = ft.calculate_feature_matrix(features=features, entityset=es,\n",
    "                                                            cutoff_time=cutoff_time,\n",
    "                                                            approximate=ft.Timedelta(6, 'd'),\n",
    "                                                            verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CANCELLED</th>\n",
       "      <th>LATE_AIRCRAFT_DELAY</th>\n",
       "      <th>flight_id</th>\n",
       "      <th>MONTH(FL_DATE)</th>\n",
       "      <th>NAS_DELAY</th>\n",
       "      <th>TAXI_OUT</th>\n",
       "      <th>YEAR(FL_DATE)</th>\n",
       "      <th>ACTUAL_ELAPSED_TIME</th>\n",
       "      <th>WHEELS_ON</th>\n",
       "      <th>Unnamed: 27</th>\n",
       "      <th>...</th>\n",
       "      <th>flights.MEAN(trips.DISTANCE)</th>\n",
       "      <th>flights.MIN(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MIN(trips.NAS_DELAY)</th>\n",
       "      <th>flights.SKEW(trips.Unnamed: 0)</th>\n",
       "      <th>flights.COUNT(trips)</th>\n",
       "      <th>flights.SUM(trips.CRS_ARR_TIME)</th>\n",
       "      <th>flights.SKEW(trips.WHEELS_ON)</th>\n",
       "      <th>flights.MAX(trips.SECURITY_DELAY)</th>\n",
       "      <th>flights.MEAN(trips.Unnamed: 27)</th>\n",
       "      <th>flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>instance_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>199995</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N13997_4220</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>113.0</td>\n",
       "      <td>1325.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>667.0</td>\n",
       "      <td>1157.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2507</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199996</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14105_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>75.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>253.0</td>\n",
       "      <td>754.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>810</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199997</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N19554_4221</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>82.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>489.0</td>\n",
       "      <td>1011.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1030</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199998</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N16911_4222</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>127.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>844.0</td>\n",
       "      <td>1312.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1333</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199999</th>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>EV_N14171_4223</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2016</td>\n",
       "      <td>92.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>427.0</td>\n",
       "      <td>734.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>758</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 163 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             CANCELLED  LATE_AIRCRAFT_DELAY       flight_id  MONTH(FL_DATE)  \\\n",
       "instance_id                                                                   \n",
       "199995             0.0                  NaN  EV_N13997_4220               2   \n",
       "199996             0.0                  NaN  EV_N14105_4221               2   \n",
       "199997             0.0                  NaN  EV_N19554_4221               2   \n",
       "199998             0.0                  NaN  EV_N16911_4222               2   \n",
       "199999             0.0                  NaN  EV_N14171_4223               2   \n",
       "\n",
       "             NAS_DELAY  TAXI_OUT  YEAR(FL_DATE)  ACTUAL_ELAPSED_TIME  \\\n",
       "instance_id                                                            \n",
       "199995             NaN      17.0           2016                113.0   \n",
       "199996             NaN      20.0           2016                 75.0   \n",
       "199997             NaN      13.0           2016                 82.0   \n",
       "199998             NaN      11.0           2016                127.0   \n",
       "199999             NaN      10.0           2016                 92.0   \n",
       "\n",
       "             WHEELS_ON  Unnamed: 27                    ...                     \\\n",
       "instance_id                                            ...                      \n",
       "199995          1325.0          NaN                    ...                      \n",
       "199996           754.0          NaN                    ...                      \n",
       "199997          1011.0          NaN                    ...                      \n",
       "199998          1312.0          NaN                    ...                      \n",
       "199999           734.0          NaN                    ...                      \n",
       "\n",
       "             flights.MEAN(trips.DISTANCE)  flights.MIN(trips.WHEELS_ON)  \\\n",
       "instance_id                                                               \n",
       "199995                              667.0                        1157.0   \n",
       "199996                              253.0                         754.0   \n",
       "199997                              489.0                        1011.0   \n",
       "199998                              844.0                        1312.0   \n",
       "199999                              427.0                         734.0   \n",
       "\n",
       "             flights.MIN(trips.NAS_DELAY)  flights.SKEW(trips.Unnamed: 0)  \\\n",
       "instance_id                                                                 \n",
       "199995                                NaN                             0.0   \n",
       "199996                                NaN                             0.0   \n",
       "199997                                NaN                             0.0   \n",
       "199998                                NaN                             0.0   \n",
       "199999                                NaN                             0.0   \n",
       "\n",
       "             flights.COUNT(trips)  flights.SUM(trips.CRS_ARR_TIME)  \\\n",
       "instance_id                                                          \n",
       "199995                          2                             2507   \n",
       "199996                          1                              810   \n",
       "199997                          1                             1030   \n",
       "199998                          1                             1333   \n",
       "199999                          1                              758   \n",
       "\n",
       "            flights.SKEW(trips.WHEELS_ON)  flights.MAX(trips.SECURITY_DELAY)  \\\n",
       "instance_id                                                                    \n",
       "199995                                0.0                                NaN   \n",
       "199996                                0.0                                NaN   \n",
       "199997                                0.0                                NaN   \n",
       "199998                                0.0                                NaN   \n",
       "199999                                0.0                                NaN   \n",
       "\n",
       "             flights.MEAN(trips.Unnamed: 27)  \\\n",
       "instance_id                                    \n",
       "199995                                   NaN   \n",
       "199996                                   NaN   \n",
       "199997                                   NaN   \n",
       "199998                                   NaN   \n",
       "199999                                   NaN   \n",
       "\n",
       "             flights.N_UNIQUE(trips.WEEKDAY(FL_DATE))  \n",
       "instance_id                                            \n",
       "199995                                              2  \n",
       "199996                                              1  \n",
       "199997                                              1  \n",
       "199998                                              1  \n",
       "199999                                              1  \n",
       "\n",
       "[5 rows x 163 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_matrix_approximated_2.tail(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Appendix**\n",
    "\n",
    "Below is a reference for filtering the data to ensure there is at least one data point before or on the approximate date when the features for a trip are approximated.\n",
    "\n",
    "We merge the 'first_trips_time' field from the flights entity into the trips dataframe.  This let's us know, for each trip, what the oldest data is for that flight."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "flight_id\n",
       "AA_N002AA_139    2016-02-01\n",
       "AA_N004AA_1258   2016-02-01\n",
       "AA_N004AA_1494   2016-02-01\n",
       "AA_N004AA_182    2016-02-01\n",
       "AA_N004AA_183    2016-02-01\n",
       "Name: first_trips_time, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flights = es['flights'].df\n",
    "flights['first_trips_time'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>flight_id</th>\n",
       "      <th>FL_DATE</th>\n",
       "      <th>first_trips_time</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trip_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>DL_N818DA_1592</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>DL_N831DN_1593</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DL_N905DL_1594</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DL_N698DL_1595</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>DL_N982AT_1596</td>\n",
       "      <td>2016-02-01</td>\n",
       "      <td>2016-02-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              flight_id    FL_DATE first_trips_time\n",
       "trip_id                                            \n",
       "0        DL_N818DA_1592 2016-02-01       2016-02-01\n",
       "1        DL_N831DN_1593 2016-02-01       2016-02-01\n",
       "2        DL_N905DL_1594 2016-02-01       2016-02-01\n",
       "3        DL_N698DL_1595 2016-02-01       2016-02-01\n",
       "4        DL_N982AT_1596 2016-02-01       2016-02-01"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_trips = trips[['flight_id', 'FL_DATE']].merge(flights[['first_trips_time']], how='left',left_on=['flight_id'], right_index=True)\n",
    "first_trips.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we filter the trips based on the time they are approximated: for each trip, the flight it belongs to must have at least one trip occur before the approximate cutoff time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "trip_by_cutoff_date = first_trips[first_trips['first_trips_time'] <= first_trips['FL_DATE'].apply(ft.bin_cutoff_time, args=(ft.Timedelta(6, 'd'),))].index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instance_id</th>\n",
       "      <th>time</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trip_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15202</th>\n",
       "      <td>15202</td>\n",
       "      <td>2016-02-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15203</th>\n",
       "      <td>15203</td>\n",
       "      <td>2016-02-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15204</th>\n",
       "      <td>15204</td>\n",
       "      <td>2016-02-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15205</th>\n",
       "      <td>15205</td>\n",
       "      <td>2016-02-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15206</th>\n",
       "      <td>15206</td>\n",
       "      <td>2016-02-02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         instance_id       time\n",
       "trip_id                        \n",
       "15202          15202 2016-02-02\n",
       "15203          15203 2016-02-02\n",
       "15204          15204 2016-02-02\n",
       "15205          15205 2016-02-02\n",
       "15206          15206 2016-02-02"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "approx_cutoff_time = trips[trips['trip_id'].isin(trip_by_cutoff_date)].filter(['trip_id', 'FL_DATE'])\n",
    "approx_cutoff_time.rename(columns={'trip_id': 'instance_id', 'FL_DATE': 'time'}, inplace=True)\n",
    "approx_cutoff_time.head(5)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
